import Koa from 'koa'
import Router from 'koa-router'
import mockList from './index.js'

const app = new Koa()
const router = new Router()

async function getRes(fn, ctx) {
  return new Promise(resolve => {
    setTimeout(() => {
      const res = fn(ctx)
      resolve(res)
    }, 1000)
  })
}

// type Method = 'get'

// 注册 mock 路由
mockList.forEach(item => {
  const { url, method, response } = item
  router[method /* as Method */](url, async (ctx /* any */) => {
    const res = await getRes(response, ctx)
    ctx.body = res
  })
})

app.use(router.routes())
app.listen(3001)
